package ae.pcfc.cldos.online.web.factories;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ae.pcfc.cldos.online.common.clddb.domain.ApplicationStatus;
import ae.pcfc.cldos.online.common.clddb.domain.Process;
import ae.pcfc.cldos.online.common.clddb.domain.ServiceMaster;
import ae.pcfc.cldos.online.common.vo.AppMetaDataVO;
import ae.pcfc.cldos.online.web.ViewPath;

/**
 * 
 * @author arosa
 *
 */
public class TaskUrlFactory {

	private static List<TaskUrlFactory.ParamsAndUrl> paramsAndUrlsCache = new ArrayList<TaskUrlFactory.ParamsAndUrl>();
	
	
	static{
		/**
		 * URL FOR ADMIN APPROVE
		 */
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA}, 
				new Long[]{ServiceMaster.SERVICE_CODE_EMP_INSIDE, ServiceMaster.SERVICE_CODE_EMP_OUTSIDE,
					ServiceMaster.SERVICE_GTH, ServiceMaster.SERVICE_INTERNAL_TRANSFER}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_EMPLOYMENT_VISA_REVIEW+"/form.htm");
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA}, 
				new Long[]{ServiceMaster.SERVICE_CODE_EMP_INSIDE, ServiceMaster.SERVICE_CODE_EMP_OUTSIDE,
					ServiceMaster.SERVICE_GTH, ServiceMaster.SERVICE_INTERNAL_TRANSFER}, 
				new Long[]{ApplicationStatus.PAYMENT_CANCELATION, ApplicationStatus.PAYMENT_RENEWAL}, 
				ViewPath.__ENTRY_PERMIT_SUBMIT+"/form.htm");
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA}, 
				new Long[]{ServiceMaster.SERVICE_CODE_EMP_INSIDE, ServiceMaster.SERVICE_CODE_EMP_OUTSIDE,
					ServiceMaster.SERVICE_GTH, ServiceMaster.SERVICE_INTERNAL_TRANSFER}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__COMPANY_EMPLOYMENT_VISA_EDIT+"/form.htm");
		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_RENEWAL}, 
				new Long[]{ServiceMaster.EV_RENEVAL}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__EV_RENEWAL+"/resubmitForm.htm");
		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_RENEWAL}, 
				new Long[]{ServiceMaster.EV_RENEVAL}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_EMPLOYMENT_VISA_RENEWAL_REVIEW+"/form.htm");

		

		addNewUrl(new String[]{Process.PROCESS_VISIT_VISA}, 
				new Long[]{ServiceMaster.NV_ONE_MONTH}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_VISIT_VISA_REVIEW+"/form.htm");
		
		addNewUrl(new String[]{Process.PROCESS_VISIT_VISA}, 
				new Long[]{ServiceMaster.NV_THREE_MONTH}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_VISIT_VISA_REVIEW+"/form.htm");
		
		addNewUrl(new String[]{Process.PROCESS_VISIT_VISA}, 
				new Long[]{ServiceMaster.NV_ONE_MONTH}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__COMPANY_VISIT_VISA_REGISTRATION+"/resubmitForm.htm?empsource=1");
		
		addNewUrl(new String[]{Process.PROCESS_VISIT_VISA}, 
				new Long[]{ServiceMaster.NV_THREE_MONTH}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__COMPANY_VISIT_VISA_REGISTRATION+"/resubmitForm.htm?empsource=3");

		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_CANCELATION}, 
				new Long[]{ServiceMaster.EV_CANCELATION}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_EMPLOYMENT_VISA_TERMINATE_REVIEW+"/form.htm");
		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_CANCELATION}, 
				new Long[]{ServiceMaster.EV_CANCELATION}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__EV_TERMINATION+"/resubmitForm.htm");
		
		addNewUrl(new String[]{Process.PROCESS_CANCEL_VISIT_VISA}, 
				new Long[]{ServiceMaster.CANCEL_VV}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_EMPLOYMENT_VISA_CANCEL_REVIEW+"/form.htm");
		
		addNewUrl(new String[]{Process.PROCESS_CANCEL_VISIT_VISA}, 
				new Long[]{ServiceMaster.CANCEL_VV}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__VV_CANCEL+"/resubmitForm.htm");
		
		/** noc letters **/
		addNewUrl(new String[]{Process.PROCESS_NOC_LETTERS}, 
				new Long[]{ServiceMaster.NOC_LETTER}, 
				new Long[]{ApplicationStatus .STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath .__NOC_LETTERS_SUBMITION+"/resubmitForm.htm");
		
		addNewUrl(new String[]{Process.PROCESS_NOC_LETTERS}, 
				new Long[]{ServiceMaster.NOC_LETTER}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__NOC_LETTERS_MAINADMIN+"/form.htm");
		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_CANCELATION}, 
				new Long[]{ServiceMaster.EV_CANCELATION}, 
				new Long[]{ApplicationStatus.STATUS_EXIT_PAPER_SUBMIT}, 
				ViewPath.__ADMIN_EMPLOYMENT_VISA_TERMINATE_EXIT_PAPER+"/form.htm");
		
/*
 * 
 * 
 *
 * 
 * 
		
		public static final String __NOC_LETTERS_SUBMITION = "client/company/nocandletters/submition";
		public static final String __NOC_LETTERS_MAINADMIN = "client/company/nocandletters/mainadmin";		
		
		public static final String __ADMIN_EMPLOYMENT_VISA_RENEWAL_REVIEW = "client/company/evrenewal/mainadmin";
		public static final String __ADMIN_EMPLOYMENT_VISA_RENEWAL_BACKOFFICE = "admin/general/backoffice/evrenewal/review";
		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_RENEWAL}, 
				new Long[]{ServiceMaster.EV_RENEVAL}, 
				new Long[]{ApplicationStatus.STATUS_REJECTED_BY_ONLINE_ADMIN, ApplicationStatus.STATUS_REJECTED_BY_GENERAL_ADMIN}, 
				ViewPath.__EV_RENEWAL+"/resubmitForm.htm");
		
		addNewUrl(new String[]{Process.PROCESS_EMPLOYMENT_VISA_RENEWAL}, 
				new Long[]{ServiceMaster.EV_RENEVAL}, 
				new Long[]{ApplicationStatus.STATUS_INITIATED, ApplicationStatus.STATUS_RESUBMITED}, 
				ViewPath.__ADMIN_EMPLOYMENT_VISA_RENEWAL_REVIEW+"/form.htm");
*/		
		
	}
	
	private static void addNewUrl(String[] processCodes, Long[] serviceCodes, Long[] statusCodes, String url){
		ParamsAndUrl paramsAndUrl = new ParamsAndUrl();
		paramsAndUrl.setProcessCodes(new HashSet<String>(Arrays.asList(processCodes)));
		paramsAndUrl.setServiceCodes(new HashSet<Long>(Arrays.asList(serviceCodes)));
		paramsAndUrl.setStatusCodes(new HashSet<Long>(Arrays.asList(statusCodes)));
		paramsAndUrl.setUrl(url);
		paramsAndUrlsCache.add(paramsAndUrl);
	}
	
	public static String getUrlByAppMetaDataVO(AppMetaDataVO appMetaDataVO){
		String result = null;
		String processCode = appMetaDataVO.getProcessCode();
		Long serviceCode = appMetaDataVO.getServiceCode();
		Long statusCode = appMetaDataVO.getStatusCode();
		for(ParamsAndUrl paramsAndUrl : paramsAndUrlsCache){
			Set<String> processCodes = paramsAndUrl.getProcessCodes();
			Set<Long> serviceCodes = paramsAndUrl.getServiceCodes();
			Set<Long> statusCodes = paramsAndUrl.getStatusCodes();
			if(processCodes.contains(processCode) &&
					serviceCodes.contains(serviceCode) &&
					statusCodes.contains(statusCode)){
				result = paramsAndUrl.getUrl();
			}
		}
		return result;
	}
	
	
	public static class ParamsAndUrl{
		
		private Set<Long> serviceCodes;
		private Set<Long> statusCodes;
		private Set<String> processCodes;
		private String url;
		
		public Set<Long> getServiceCodes() {
			return serviceCodes;
		}
		public void setServiceCodes(Set<Long> serviceCodes) {
			this.serviceCodes = serviceCodes;
		}
		public Set<Long> getStatusCodes() {
			return statusCodes;
		}
		public void setStatusCodes(Set<Long> statusCodes) {
			this.statusCodes = statusCodes;
		}
		public Set<String> getProcessCodes() {
			return processCodes;
		}
		public void setProcessCodes(Set<String> processCodes) {
			this.processCodes = processCodes;
		}
		public String getUrl() {
			return url;
		}
		public void setUrl(String url) {
			this.url = url;
		}
	}
}
